<?php
namespace App\Repositories\Finance;

use App\Models\Finance\FinanceThirdModel;
use App\Repositories\BaseRepository;

class FinanceThirdRepository extends BaseRepository
{
    protected $repository;

    public function __construct()
    {
        $this->repository = new FinanceThirdModel();
    }

    public function getAdminIndex()
    {
        $models = $this->repository->paginate(10);
        $models->limit = 10;
        return $models;
    }

    /**
     * 账单历史
     * @param $operType
     * @param int $page
     * @return mixed
     */
    public function getApiIndex($operType,$page=1)
    {
        $pageLimit = 10;
        $pageStart = ($page - 1) * $pageLimit;
        $query = $this->repository->where('status',2);
        if ($operType=='income') {
            $query = $query->where('genre',1);
        } else {
            $query = $query->where('genre',2);
        }
        $models = $query->orderBy('id','desc')
            ->skip($pageStart)
            ->take($pageLimit)
            ->get();
        return $models;
    }

    public function setAdd($data)
    {
        $data['created_at'] = time();
        return $this->repository->insertGetId($data);
    }

    public function setModify($data,$orderId)
    {
        $data['updated_at'] = time();
        return $this->repository->where('bale',$orderId)
            ->update($data);
    }

    public function setModifyBySerial($data,$serial)
    {
        $data['updated_at'] = time();
        return $this->repository->where('serial',$serial)
            ->update($data);
    }

    public function getShowOne($id)
    {
        return $this->repository->where('id',$id)->first();
    }

    public function getOneBySerial($serial)
    {
        return $this->repository->where('serial',$serial)->first();
    }

    /**
     * 根据状态，获取支付订单
     */
    public function getOneByStatus($uid,$genre,$status)
    {
        return $this->repository
            ->where('uid',$uid)
            ->where('genre',$genre)
            ->where('status',$status)
            ->first();
    }
}